package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiGoodsTopicLink;
import cn.com.api.entity.ApiGoods_p;
import cn.com.api.entity.ApiSpeicalChang;
import cn.com.api.entity.ApiT_goods_p;
import cn.com.api.entity.ApiT_goods_tags_link;
import cn.com.api.entity.ApiT_goods_topic;
import cn.com.api.entity.ApiT_member_entrepreneur;
import cn.com.api.entity.ApiT_products;
import cn.com.api.entity.ApiType;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.ApiU_follow;
import cn.com.api.entity.apiGoodInfo;
import cn.com.api.mapper.ApiDianPingMapper;
import cn.com.api.mapper.ApiGoodsMapper;
import cn.com.api.mapper.ApiT_goods_pMapper;
import cn.com.api.mapper.ApiT_goods_tags_linkMapper;
import cn.com.api.mapper.ApiT_goods_topicMapper;
import cn.com.api.mapper.ApiT_logMapper;
import cn.com.api.mapper.ApiT_member_entrepreneurMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.mapper.ApiTypeMapper;
import cn.com.api.mapper.ApiU_favoriteMapper;
import cn.com.api.mapper.ApiU_followMapper;
import cn.com.api.resultpage.GoodsPage;
import cn.com.api.resultpage.InitMyOrderPage;
import cn.com.api.resultpage.InitShopGoodsPage;
import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiTypeService;
import cn.com.util.ArithUtil;
import cn.com.util.PageInfo;
import cn.com.util.Util;
import cn.com.util.Utils;

@Service
public class ApiGoodsServiceImpl implements ApiGoodsService {
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private ApiGoodsMapper goodsMapper;
	@Autowired
	private ApiT_goods_pMapper apiT_goods_pMapper;
	@Autowired
	private ApiTypeMapper apiTypeMapper;
	@Autowired
	private ApiT_productsMapper apiT_productsMapper;
	/*
	 * @Autowired private ApiShopsMapper apiShopsMapper;
	 */
	@Autowired
	private ApiT_logMapper apiT_logMapper;
	@Autowired
	private ApiT_goods_tags_linkMapper apiT_goods_tags_linkMapper;
	@Autowired
	private ApiDianPingMapper dianPingMapper;
	@Autowired
	private ApiU_favoriteMapper favoriteMapper;
	@Autowired
	private ApiU_followMapper followMapper;
	@Autowired
	private ApiT_goods_topicMapper goods_topicMapper;
	@Autowired
	private ApiT_member_entrepreneurMapper api_meMapper;

	// 根据id查询商品

	@Override
	// 条件查询
	public List<Map<String, Object>> getGoodsByType(Object goods_type, Object obj) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (goods_type != null) {
			// 获取子类
			List<Integer> child_type_code_list = new ArrayList<Integer>();
			// 加入自己
			child_type_code_list.add(Integer.parseInt(goods_type.toString()));

			List<ApiType> types = apiTypeMapper.getTypesByCBelong(Integer.parseInt(goods_type.toString()));
			for (ApiType type : types) {
				Integer type_code = type.getcCode();
				child_type_code_list.add(type_code);
			}
			list = goodsMapper.getGoodsByType(child_type_code_list, obj);
		} else {
			list = goodsMapper.getGoodsDef();
		}
		return list;
	}

	@Override
	public void searchGoodsByShopId(Map<String, String> params, Map<String, Object> map, PageInfo page)
			throws Exception {
		// 查询
		String shop_id = params.get("shop_id");
		Map<String, Object> paramData = new HashMap<String, Object>();
		paramData.put("shop_id", shop_id);

		List<ApiGoods> myGoods = goodsMapper.searchGoodsByShopIdListPage(paramData, page);

		InitMyOrderPage initMyOrderPage = new InitMyOrderPage();
		// initMyOrderPage.setMyOrders(myGoods);
		map.put("page", page);
		map.put("datas", myGoods);
	}

	// 默认查询
	@Override
	public List<Map<String, Object>> getGoodsDef() {
		// TODO
		return goodsMapper.getGoodsDef();
	}

	// 根据id查询商品
	@Override
	public List<Map<String, Object>> getGoods(Object id) {
		// TODO
		List<Map<String, Object>> list = goodsMapper.getGoodsById(id);
		//
		return list;
	}

	// 搜索商品
	@Override
	public List<Map<String, Object>> searchGoods(String name, String cityid) {
		List<Map<String, Object>> list = goodsMapper.searchGoods(name, cityid);
		return list;
	}

	// 图文详情
	@Override
	public List<Map<String, Object>> imageDetail(Object id) {
		List<Map<String, Object>> list = goodsMapper.imageDetail(id);
		return list;
	}

	// 搜索店铺
	@Override
	public List<Map<String, Object>> searchShops(String name, String cityid) {
		List<Map<String, Object>> list = goodsMapper.searchShops(name, cityid);
		return list;
	}

	@Override
	public List<ApiGoods> serchGoodsByParType(int par_type) {
		List<ApiGoods> list = goodsMapper.serchGoodsByParType(par_type);
		return list;
	}

	// 查询我收藏的商品
	public List<ApiGoods> searchMyGoods(@Param("u_id") String u_id) {
		List<ApiGoods> list = goodsMapper.searchMyGoods(u_id);
		return list;
	}

	// 查询我收藏的商品
	public List<Map<String, Object>> searchMyGoodsPage(String u_id, PageInfo page) {
		return apiT_logMapper.findT_logs4GoodsByUserIdListPage(u_id, page);
	}

	@Override
	public List<ApiGoods> tuijianGoods(String type, String cityid) {
		return goodsMapper.tuijianGoods(type, cityid);
	}

	@Override
	public List<Map<String, Object>> serchGoodsByType(int type, int city_id) {
		List<Map<String, Object>> list = goodsMapper.serchGoodsByType(type, city_id);
		return list;
	}

	@Override
	public List<ApiGoods> serchGoodsByPromotionType(int type, PageInfo pi) {
		List<ApiGoods> list = goodsMapper.serchGoodsByPromotionTypeListPage(type, pi);
		return list;
	}

	// 根据id获取
	public ApiGoods findT_goodsById(Long id) {
		ApiGoods t_good = goodsMapper.findT_goodsById(id);
		return t_good;
	}

	// 添加
	public int insertT_goods(ApiGoods t_good) {
		int id = goodsMapper.insertT_goods(t_good);
		return id;
	}

	// 添加商品所有信息 包含 图片 货品
	@Transactional
	public int insertT_goodsAll(ApiGoods t_good, String products_list, String image_list, String[] detialPics,
			String[] goodstagsid) {
		int ret = 0;

		goodsMapper.insertT_goods(t_good);
		long ret_goods_id = t_good.getId();
		long ret_product_id = 0;
		// products
		// System.out.println("ret_goods_id:--------------商品" + ret_goods_id);
		String[] str = products_list.split(";");
		if ((str != null) && (str.length > 0)) {
			for (int i = 0; i < str.length; i++) {
				ApiT_products product = new ApiT_products();
				product.setGoods_id(ret_goods_id);
				product.setSku_code("");
				product.setErp_barcode("");
				product.setErp_product_id("");
				product.setErp_goods_sn("");
				product.setSpec("");
				String[] str0 = str[i].split(",");
				product.setMkt_price(Util.getBigDecimal(str0[0]));
				product.setPrice(Util.getBigDecimal(str0[1]));
				product.setPdt_spec(str0[2]);
				product.setStore(Integer.valueOf(str0[3]));
				product.setUnit(str0[4]);
				product.setCity_id(t_good.getCity_id());
				product.setShop_id(t_good.getShop_id());
				product.setStatus(1);
				product.setSold(0);// 已售数量

				apiT_productsMapper.insertT_productsReturnId(product);
				if (i == 0) {
					ret_product_id = product.getProduct_id();
				}
			}
		}

		// update default_product_id
		ApiGoods t_good_2 = goodsMapper.findT_goodsById(ret_goods_id);
		t_good_2.setDefault_product_id(ret_product_id);
		// t_good_2.setSpec("");
		// t_good_2.setPromotion_type(0);
		// t_good_2.setPar_type(0);
		// t_good_2.setIs_top(0);
		// t_good_2.setPromotion_image("");
		// t_good_2.setIs_deleted(0);
		goodsMapper.updateT_goods(t_good_2);

		// goods' pics
		String[] img_str_list = image_list.split(",");

		if ((img_str_list != null) && (img_str_list.length > 0)) {
			for (int i = 0; i < img_str_list.length; i++) {
				if (img_str_list[i].length() > 1) {
					ApiGoods_p p = new ApiGoods_p();
					p.setGoodsId(ret_goods_id);
					p.setFileName("");
					p.setPath(img_str_list[i]);
					p.setCommit_time(new Date());
					p.setNote("");
					p.setStatus(0);
					p.setType(0);
					goodsMapper.insertGoods_p(p);
				}
			}
		}

		// 商品详情图片
		if (detialPics != null) {
			for (String pic : detialPics) {
				if (!Util.isNull(pic)) {
					ApiGoods_p p = new ApiGoods_p();
					p.setGoodsId(ret_goods_id);
					p.setFileName("");
					p.setPath(pic);
					p.setCommit_time(new Date());
					p.setNote("");
					p.setStatus(0);
					p.setType(1);
					goodsMapper.insertGoods_p(p);
				}
			}
		}
		//

		if (goodstagsid != null && goodstagsid.length != 0) {
			List<ApiT_goods_tags_link> list = new ArrayList<ApiT_goods_tags_link>();
			for (String tagid : goodstagsid) {
				ApiT_goods_tags_link apiT_goods_tags_link = new ApiT_goods_tags_link();
				apiT_goods_tags_link.setGtag_id(Integer.valueOf(tagid));
				apiT_goods_tags_link.setGoods_id(ret_goods_id);
				list.add(apiT_goods_tags_link);
			}
			apiT_goods_tags_linkMapper.insertGoodsTagsLinks(list);
		}
		ret = 1;
		return ret;
	}

	// 添加商品所有信息 包含 图片 货品
	@Transactional
	public int insertT_goodsAll4erp(ApiGoods t_good) {
		int ret = 0;

		int ret_i = goodsMapper.insertT_goods4erp(t_good);
		long ret_goods_id = t_good.getId();
		//
		ret = 1;
		return ret;
	}

	// 更新erp同步资料
	@Transactional
	public int updateT_goods4erp(ApiGoods t_good) {
		int ret_i = goodsMapper.updateT_goods4erp(t_good);
		return ret_i;
	}

	// 更新商品所有信息 包含 图片 货品
	@Transactional
	public int updateT_goodsAll(ApiGoods t_good, String products_list, String image_list, String image_list_del,
			String[] delteIds, String[] detialPics) {
		int ret = 0;

		goodsMapper.updateT_goods(t_good);
		// products
		String[] str = products_list.split(";");
		long goodId = t_good.getId();
		long ret_product_id = 0;
		// Map<String, Object> conditions = new HashMap<String, Object>();
		if ((str != null) && (str.length > 0)) {
			// apiT_productsMapper.T_productsDeleteByGoods_id(t_good.getId()+"");
			ApiT_products product = null;
			for (int i = 0; i < str.length; i++) {

				String[] str0 = str[i].split(",");

				// 根据城市id得到这个城市的自营shop
				// ApiShop ziYingShop = null;
				// if (str0.length > 6) {
				// int cityId = t_good.getCity_id();
				// try {
				// cityId = Integer.valueOf(str0[6]);// 如果前台未传cityId,则取t_goods的
				// } catch (Exception ex) {
				// }
				// conditions.put("cityId", cityId);
				// conditions.put("isZiYing", true);
				// List<ApiShop> ziYingShops =
				// apiShopsMapper.searchShops(conditions);
				// if (ziYingShops != null && ziYingShops.size() > 0) {
				// ziYingShop = ziYingShops.get(0);
				// }
				// }
				product = apiT_productsMapper.getProByProductId(str0[5]);
				if (product == null) {
					product = new ApiT_products();
					product.setGoods_id(goodId);
					product.setMkt_price(Util.getBigDecimal(str0[0]));
					product.setPrice(Util.getBigDecimal(str0[1]));
					product.setPdt_spec(str0[2]);
					product.setStore(Integer.valueOf(str0[3]));
					product.setUnit(str0[4]);
					apiT_productsMapper.insertT_productsReturnId(product);
				} else {
					product.setMkt_price(Util.getBigDecimal(str0[0]));
					product.setPrice(Util.getBigDecimal(str0[1]));
					product.setPdt_spec(str0[2]);
					product.setStore(Integer.valueOf(str0[3]));
					product.setUnit(str0[4]);
					apiT_productsMapper.updateProducts(product);
				}
				if (i == 0) {
					ret_product_id = product.getProduct_id();
				}
				// if (str0.length > 6) {
				// try {
				// product.setCity_id(Integer.valueOf(str0[6]));
				// } catch (Exception ex) {
				// product.setCity_id(t_good.getCity_id());
				// }
				// } else {
				// product.setCity_id(t_good.getCity_id());
				// }
				// product.setShop_id(t_good.getShop_id());
				// if (ziYingShop != null) {
				// product.setShop_id(ziYingShop.getcId());
				// }

			}

			// 更新商品时 不更新货品

			// Integer ret_id =
			// apiT_productsMapper.insertT_productsReturnId(product);

		}

		// update default_product_id

		// ApiGoods t_good_2 = goodsMapper.findT_goodsById(t_good.getId());
		t_good.setDefault_product_id(ret_product_id);
		goodsMapper.updateT_goods(t_good);

		// goods' pics
		String[] img_str_list = image_list.split(",");
		String[] img_del_str_list = image_list_del.split(","); // 已经删除的图片
		System.out.println("image_list:" + image_list);
		if ((img_str_list != null) && (img_str_list.length > 0)) {
			// 删除原有老图片
			int ret_del_p = apiT_goods_pMapper.deleteGoodsPic(t_good.getId() + "");
			//
			for (int i = 0; i < img_str_list.length; i++) {
				if (img_str_list[i].length() > 1) {
					// 判断是否是已删除图片
					int flag = 0;
					for (int h = 0; h < img_del_str_list.length; h++) {
						System.out.println(img_str_list[i].trim());
						System.out.println(img_del_str_list[h].trim());
						System.out.println("||");
						if (img_del_str_list[h].trim().equalsIgnoreCase(img_str_list[i].trim())) {
							flag = 1;
							break;
						}
					}
					if (flag == 0) {
						ApiGoods_p p = new ApiGoods_p();
						p.setGoodsId(t_good.getId());
						p.setFileName("");
						p.setPath(img_str_list[i].trim());
						p.setCommit_time(new Date());
						p.setNote("");
						p.setStatus(0);
						p.setType(0);
						goodsMapper.insertGoods_p(p);
					}
				}
			}
		}

		// 删除商品详情图上
		if (delteIds != null) {
			apiT_goods_pMapper.deletePicByIds(delteIds);
		}

		// 商品详情图片
		if (detialPics != null) {
			for (String pic : detialPics) {
				if (!Util.isNull(pic)) {
					ApiGoods_p p = new ApiGoods_p();
					p.setGoodsId(t_good.getId());
					p.setFileName("");
					p.setPath(pic);
					p.setCommit_time(new Date());
					p.setNote("");
					p.setStatus(0);
					p.setType(1);
					goodsMapper.insertGoods_p(p);
				}
			}
		}
		//
		ret = 1;
		return ret;
	}

	// 修改
	public int updateT_goods(ApiGoods t_good) {
		int ret_i = goodsMapper.updateT_goods(t_good);
		return ret_i;
	}

	// 删除
	public int deleteT_goods(Integer id) {
		int ret_I = goodsMapper.deleteT_goods(id);
		return ret_I;
	}

	// 添加商品图片
	public void insertGoods_p(ApiGoods_p goods_p) {
		goodsMapper.insertGoods_p(goods_p);
		return;
	}

	// 商品图库

	public List<ApiGoods_p> findGoods_pByGoodsId(Long goods_id) {
		List<ApiGoods_p> list = goodsMapper.findGoodsPByGoodsId(goods_id);
		return list;
	}

	/**
	 * 根据商品Id得到商品的所有图片（包括商品详情图片和商品多图）
	 */
	public List<ApiGoods_p> findAllGoodsPByGoodsId(Long goods_id) {
		List<ApiGoods_p> list = goodsMapper.findAllGoodsPByGoodsId(goods_id);
		return list;
	}

	public List<ApiGoods> searchGoodsByShop(int shop_id) {
		return goodsMapper.searchGoodsByShop(shop_id);
	}

	/*********** syy start ************************/
	@Override
	public List<GoodsPage> searchGoodsByGoodsIds(Integer did, String goods_ids) {
		List<GoodsPage> goodsList = null;
		if (!Util.isNull(goods_ids)) {
			String[] goods_idsStr = goods_ids.split(",");
			List<Integer> idList = new ArrayList<Integer>();
			goodsList = new ArrayList<GoodsPage>();
			if (goods_idsStr != null && goods_idsStr.length > 0) {
				for (String id : goods_idsStr) {
					Integer goods_id = Integer.parseInt(id);
					idList.add(goods_id);
				}
				goodsList = goodsMapper.searchGoodsByGoodsIds(did, idList);
			}
		}
		return goodsList;
	}

	@Override
	public List<GoodsPage> searchGoodsByOrderId(Integer did) {
		List<GoodsPage> goodsList = goodsMapper.searchGoodsByOrderId(did);
		return goodsList;
	}

	/*********** syy end ************************/

	@Override
	public List<ApiGoods> findT_goodsListOrderById() {
		//
		List<ApiGoods> goodsList = goodsMapper.findT_goodsListOrderById();
		return goodsList;
	}

	@Override
	public Map<String, Object> getGoodsByProductId(String productid) {
		Map<String, Object> result = goodsMapper.getGoodsByProductId(productid);
		if (result != null && result.size() > 0) {
			Integer isEdit = -1;
			try {
				isEdit = Integer.valueOf(result.get("isEdit").toString());
				isEdit = apiTypeService.getShopType(isEdit);
			} catch (Exception e) {

			}
			result.put("isEdit", isEdit);
		}
		return result;
	}

	@Override
	public ApiGoods searchGoodsById(Long id) {
		return goodsMapper.searchGoodsById(id);
	}

	@Override
	public List<ApiGoods> searchGoods(Map<String, Object> conditions, PageInfo pageInfo) {
		return goodsMapper.searchGoodsListPage(conditions, pageInfo);
	}

	@Override
	public List<Map<String, Object>> searActGoodsListPage(Map<String, Object> conditions, PageInfo pageInfo) {
		return goodsMapper.searActGoodsListPage(conditions, pageInfo);
	}

	@Override
	public void shopSearchGoodsByType(Map<String, String> params, Map<String, Object> map) {
		Integer shop_id = Integer.parseInt(params.get("shop_id"));
		Integer pageCount = Integer.parseInt(params.get("pageCount"));
		Integer cCode = Integer.parseInt(params.get("cCode"));

		PageInfo page = new PageInfo();
		page.setCurrentPage(pageCount);

		List<ApiGoods> goodsList = goodsMapper.searchGoodsByShopAndTypeListPage(shop_id, cCode, page);

		if (goodsList != null && goodsList.size() > 0) {
			for (ApiGoods goods : goodsList) {// 查询图片库+货品：商品的各个规格
				List<ApiT_goods_p> imageList = new ArrayList<ApiT_goods_p>();
				imageList = apiT_goods_pMapper.findIntroduceGoodsImage(goods.getId() + "");
				goods.setImageList(imageList);

				List<ApiT_products> productList = new ArrayList<ApiT_products>();
				productList = apiT_productsMapper.findT_productssByGoods_id(goods.getId() + "");
				goods.setProductList(productList);
			}

		}

		// 查询商品
		InitShopGoodsPage initShopGoodsPage = new InitShopGoodsPage();
		initShopGoodsPage.setList(goodsList);
		initShopGoodsPage.setTotalPage(page.getTotalPage());

		map.put("datas", initShopGoodsPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");

	}

	@Override
	public void updateGoodsTopOrDel(Map<String, String> params, Map<String, Object> map) {
		String id_list = params.get("id_list");
		String status = params.get("status");
		String is_deleted = params.get("is_deleted");

		String[] ids = null;
		if (!Util.isNull(id_list)) {
			ids = id_list.split(",");
		}
		if (ids != null && ids.length > 0) {
			if (!Util.isNull(status)) {

				for (String id : ids) {// 上下架
					Map<String, Object> paramsDate = new HashMap<String, Object>();
					paramsDate.put("goods_id", id);
					paramsDate.put("status", status);
					goodsMapper.updateGoodsTopOrDel(paramsDate);
				} // for
			}
			if (!Util.isNull(is_deleted)) {
				for (String id : ids) {// 是否删除
					Map<String, Object> paramsDate = new HashMap<String, Object>();
					paramsDate.put("goods_id", id);
					paramsDate.put("is_deleted", is_deleted);
					goodsMapper.updateGoodsTopOrDel(paramsDate);
				} // for
			}

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "更新成功！");
		}
	}

	@Override
	public List<Map<String, String>> searShopAllGoodsListPage(String shopid, PageInfo page) {
		return goodsMapper.searShopAllGoodsListPage(shopid, page);
	}

	@Override
	public Map<String, Object> fujinGoods(String type, String pageCount, String cityid, String obj) {
		List<Map<String, Object>> md = new ArrayList<Map<String, Object>>();
		List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
		PageInfo page = new PageInfo();
		Map<String, Object> res = new HashMap<String, Object>();
		List<String> shopid = new ArrayList<String>();
		int count = 0;
		try {
			md = goodsMapper.searMenGoods(type, cityid);
			for (Map<String, Object> m : md) {
				count += Integer.parseInt(m.get("count") + "");
				shopid.add(m.get("shop_id") + "");
			}
			page.setCurrentPage(Integer.parseInt(pageCount));
			datas = goodsMapper.menAllGoodsListPage(page, obj, shopid, type, cityid);
			res.put("datas", datas);
			res.put("shopNum", md.size());// 附近门店数
			res.put("goodNum", count);// 附件商品数
			res.put("page", page);
			res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			res.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			e.printStackTrace();
			res.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			res.put(Utils.MESSAGE, "系统异常！");
		}
		return res;
	}

	@Override
	public Map<String, Object> fujinShop(String type, String cityid, String jd, String wd) {
		List<Map<String, Object>> md = new ArrayList<Map<String, Object>>();
		List<Object> datas = new ArrayList<Object>();
		Map<String, Object> res = new HashMap<String, Object>();
		int count = 0;
		try {
			md = goodsMapper.searMenGoods(type, cityid);
			for (Map<String, Object> m : md) {
				Map<String, Object> shoplist = new HashMap<String, Object>();
				shoplist.put("name", m.get("c_sname"));
				shoplist.put("shopid", m.get("shop_id"));
				shoplist.put("c_address", m.get("c_address"));
				shoplist.put("count", m.get("count"));
				shoplist.put("image", m.get("c_logo"));
				shoplist.put("distance", Utils.GetDistance(wd, jd, m.get("latitude") + "", m.get("longitude") + ""));
				shoplist.put("goods", goodsMapper.menThreeGoods(m.get("shop_id") + "", type));
				count += Integer.parseInt(m.get("count") + "");
				datas.add(shoplist);
			}
			res.put("datas", datas);
			res.put("shopNum", md.size());
			res.put("goodNum", count);
			res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			res.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			e.printStackTrace();
			res.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			res.put(Utils.MESSAGE, "系统异常！");
		}
		return res;
	}

	@Override
	public List<Map<String, Object>> searActGoodsByTypeListPage(Map<String, Object> conditions, PageInfo pageInfo) {
		// TODO
		return goodsMapper.searActGoodsByTypeListPage(conditions, pageInfo);
	}

	@Override
	public Map<String, Object> shareProInfo(String proid) {
		return goodsMapper.shareProInfo(proid);
	}

	@Override
	public Map<String, Object> shareProDetail(@Param("proid") String proid) {
		return goodsMapper.shareProDetail(proid);
	}

	@Override
	public List<Map<String, Object>> searGoodsActTypeListPage(String typeid, String cityid, String name,
			PageInfo pageInfo) {
		return goodsMapper.searGoodsActTypeListPage(typeid, cityid, name, pageInfo);
	}

	@Override
	public Integer getSoldedGoodsCount(long goodsId) {
		return goodsMapper.getSoldedGoodsCount(goodsId);
	}

	@Override
	public int batchUpdateGoodsStatus(Long[] goodsIds, int status) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("goodsIds", goodsIds);
		condition.put("status", status);
		return goodsMapper.batchUpdateGoods(condition);
	}

	/**
	 * 查询店铺的热销产品
	 * 
	 * @param shop_id
	 * @param page
	 * @return
	 */
	@Override
	public List<Map<String, Object>> searchHotSaleGoodsByShopId(String shop_id, PageInfo page) {
		// 销量大于10的商品--在售中的商品 热销
		List<Map<String, Object>> list = goodsMapper.searchHotSaleGoodsByShopIdListPage(shop_id, 10, page);
		return list;
	}

	@Override
	public List<Map<String, Object>> getAllGoodTypes() {
		// TODO Auto-generated method stub
		return goodsMapper.getAllGoodsType();
	}

	@Override
	public LinkedList<ApiSpeicalChang> searchSpecailChangList(Map<String, Object> conditions, PageInfo pageInfo) {
		return goodsMapper.searchSpecailChangListPage(conditions, pageInfo);
	}

	@Override
	public void addSpeicalChang(ApiSpeicalChang apiSpeicalChang) {
		goodsMapper.addSpecalChang(apiSpeicalChang);

	}

	@Override
	public List<ApiGoods> searchGoodsByType(int typeId, String topicId, String flag, String userId) {
		return goodsMapper.searchScByTypeId(typeId, topicId, flag, userId);
	}

	@Override
	public void batchInsertTopicLink(List<ApiGoodsTopicLink> list) {
		goodsMapper.batchInsertTopicLink(list);
	}

	@Override
	public List<Map<String, Object>> getGoodInfoByCondition(String key, String is_recommend) {
		// TODO Auto-generated method stub
		Map<String, Object> parms = new HashMap<String, Object>();
		parms.put("key", key);
		if (is_recommend != null && !"".equals(is_recommend)) {
			parms.put("is_recommend", is_recommend);
		}
		List<Map<String, Object>> listMap = goodsMapper.getGoodInfoByCondition(parms);
		for (Map<String, Object> map : listMap) {
			map.put("DISPERCENT", Integer.parseInt(map.get("disPercent").toString()));
			map.put("COUNT", Integer.parseInt(map.get("count").toString()));
			map.put("ID", Integer.parseInt(map.get("id").toString()));
			map.put("STORE", Integer.parseInt(map.get("store").toString()));
		}
		return listMap;
	}

	@Override
	public Map<String, Object> getGoodInfoByGoodId(int goodId, String userIdPass, String topicId) {
		Map<String, Object> map = new HashMap<>();
		if (!Util.isNull(userIdPass)) {
			Map<String, Object> conditionMap = new HashMap<>();
			conditionMap.put("goodId", goodId);
			conditionMap.put("userId", userIdPass);
			conditionMap.put("type", 1);
			ApiU_favorite favorite = favoriteMapper.getFavoriteByCondition(conditionMap);
			if (Util.isNull(favorite)) {
				map.put("isSave", 0);
			} else {
				map.put("isSave", 1);
			}
		}
		List<apiGoodInfo> goodMap = goodsMapper.getGoodsInfoByGoodId(goodId);
		ApiGoods apiGoods = goodsMapper.findT_goodsById(Long.valueOf(goodId));
		String userid = apiGoods.getUserId();
		ApiT_member_entrepreneur member_entrepreneur = api_meMapper.findMemberEntById(userid);
		// ApiU_follow follow = followMapper.getFollowByCondition(userIdPass,
		// enterId);
		ApiU_follow follow = followMapper.getIsFollow(userIdPass, member_entrepreneur.getId() + "", 1);// 是否关注企业主
		if (!Util.isNull(follow)) {
			map.put("isFollow", 1);
		} else {
			map.put("isFollow", 0);
		}
		if (!Util.isNull(member_entrepreneur)) {
			map.put("corpSname", member_entrepreneur.getCorp_sname());
			map.put("logoImg", member_entrepreneur.getLogo_img());
			map.put("id", userid);
			map.put("entrepreneurid", member_entrepreneur.getId());
			map.put("userMobile", member_entrepreneur.getUser_mobile());
			map.put("corpBrief", member_entrepreneur.getCorp_brief());
			String addressArr[] = member_entrepreneur.getAddress().split("%");
			map.put("province", addressArr[0]);
			map.put("city", addressArr[1]);
			Map<String, Object> dianpMap = dianPingMapper.getDianPingPointByUserId(userid);
			double sumGoods = Double.valueOf(dianpMap.get("sumLevelGoods").toString());
			double count = Double.valueOf(dianPingMapper.getCountDianPingByUserId(userid));
			if (count == 0.0) {
				count = 1.0;
			}
			map.put("goodsPer", ArithUtil.round(sumGoods / count, 1));
		} else {
			map.put("corpSname", "");
			map.put("logoImg", "");
			map.put("id", "");
			map.put("userMobile", "");
			map.put("corpBrief", "");
			map.put("province", "");
			map.put("city", "");
			map.put("goodsPer", "");
		}
		map.put("saleNum", goodsMapper.getSaleNumByGoodId(goodId + ""));
		map.put("goodInfo", goodMap);
		map.put("showPic", goodsMapper.getGoodPicByGoodId(goodId + "", 0));
		map.put("detailPic", goodsMapper.getGoodPicByGoodId(goodId + "", 1));
		// ApiT_goods_topic goods_topic =
		// goods_topicMapper.getTopicByTopicId(topicId);
		// map.put("startTime", goods_topic.getStart_time());
		// map.put("endTime", goods_topic.getEnd_time());
		// map.put("startDate", goods_topic.getStart_date());
		return map;
	}

	@Override
	public List<ApiGoods> findGoodsList(String user_id, Integer type, PageInfo page) {
		// TODO Auto-generated method stub
		return goodsMapper.findGoodsListByFavoriteTypeListPage(user_id, type, page);
	}

}
